package com.lhy.tool.database.converter.mysql;

import com.lhy.tool.database.DbColumnType;
import com.lhy.tool.database.converter.ColumnTypeMappingRule;
import com.lhy.tool.database.converter.common.AbstractDbColumnTypeMapping;
import com.lhy.tool.database.converter.common.DbDateConverter;
import com.lhy.tool.database.converter.common.DbTimeConverter;
import com.lhy.tool.database.converter.common.DbYearConverter;

public class MysqlColumnTypeMapping extends AbstractDbColumnTypeMapping {

    public MysqlColumnTypeMapping() {
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("char") || columnType.contains("text")
                || columnType.contains("json") || columnType.contains("enum"), (columnType) -> DbColumnType.STRING));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("bigint"), (columnType) -> DbColumnType.LONG));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("tinyint(1)") || columnType.contains("bit"), (columnType) -> DbColumnType.BOOLEAN));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("int"), (columnType) -> DbColumnType.INTEGER));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("decimal"), (columnType) -> DbColumnType.BIG_DECIMAL));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("clob"), (columnType) -> DbColumnType.CLOB));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("blob"), (columnType) -> DbColumnType.BLOB));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("binary"), (columnType) -> DbColumnType.BYTE_ARRAY));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("float"), (columnType) -> DbColumnType.FLOAT));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("double"), (columnType) -> DbColumnType.DOUBLE));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("datetime") || columnType.contains("timestamp"), new MysqlDateTimeConverter()));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("date"), new DbDateConverter()));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("time"), new DbTimeConverter()));
        rules.add(new ColumnTypeMappingRule(columnType -> columnType.contains("year"), new DbYearConverter()));
    }

}
